package com.squapsource.project_euler;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.apache.commons.lang.StringUtils;

public class Problem26 {

	private static final int MAX = 1000;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int maxRepeat = 0;
		int maxNum = 0;
		for (int i = 2; i < MAX; i++) {
			BigDecimal fraction = BigDecimal.ONE.divide(BigDecimal.valueOf(i),
					2000, RoundingMode.HALF_DOWN);
			fraction.stripTrailingZeros();
			String decimal = fraction.toPlainString().split("\\.")[1];
			decimal = decimal.substring(0, decimal.length() - 1);
			decimal = decimal.replaceAll("0*$", "");
			String buffer = "";
			for (int c = 0; c < decimal.length(); c++) {
				buffer += decimal.charAt(c);
				if (!decimal.substring(c + 1, decimal.length())
						.contains(buffer)) {
					buffer = "";
					continue;
				}

				int repeatSize = (decimal.length() - c) / buffer.length();
				String compareString = decimal.substring(c + 1, Math.min(
						(c + 1) + buffer.length() * repeatSize,
						decimal.length()));
				if (compareString
						.equals(StringUtils.repeat(buffer, repeatSize))) {
					if (buffer.length() > maxRepeat) {
						maxRepeat = buffer.length();
						maxNum = i;
					}
					break;
				}
			}

		}
		System.out.println(maxNum + " " + maxRepeat);
	}

}
